from sklearn.feature_selection import SelectKBest, r_regression
from sklearn.datasets import load_iris
import pandas as pd

# 加载数据
data = load_iris()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)

# 选择与 y 最相关的前 4 个特征
selector = SelectKBest(score_func=r_regression, k=4)
X_sel = selector.fit_transform(X, y)
selected_features = X.columns[selector.get_support()]
print("Selected features:", selected_features.tolist())